package com.tongzhan.controller;

import com.tongzhan.base.Return;
import com.tongzhan.entity.SysDict;
import com.tongzhan.service.SysDictService;
import com.tongzhan.utils.SnowFlakeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 系统枚举配置
 * @author htmic
 * @date 2020/7/31
 */
@RequestMapping("/sysDict")
@RestController
public class SysDictController {

    @Autowired
    private SysDictService sysDictService;

    /**
     * 获取枚举配置数据
     * @param code
     * @return
     */
    @PostMapping("/listByCode")
    public Return listByCode(String code) {
        if (StringUtils.isEmpty(code)) {
            return Return.getErrorResult("缺少参数");
        }
        List<SysDict> list = sysDictService.getListByPcode(code);
        return Return.getSuccessResult(list);
    }

    /**
     * 添加
     * @param pid 父主键 ，根目录则设置为0
     * @param code
     * @param type 枚举值
     * @param value 枚举值名字
     * @param desc 描述
     * @return
     */
    @PostMapping("/addDict")
    public Return addDict(String pid,String code,String type,String value,String desc) {
        if (pid == null) {
            return Return.getErrorResult("缺少参数");
        }
        SysDict sysDict = new SysDict();
        sysDict.setPid(pid);
        sysDict.setCode(code);
        sysDict.setType(type);
        sysDict.setValue(value);
        sysDict.setDesc(desc);
        sysDict.setId(new SnowFlakeUtils(3, 3).nextId()+"");
        sysDictService.addDict(sysDict);
        return Return.getSuccessResult();
    }
}
